 program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement = ^element;
  element = record
    a : Integer;
    b : integer;
    next : pelement;
  end;

var
  fin : textfile;
  n, i, k, l, m, o, p: integer;
  head1, head2 : pelement;

procedure Add (var head : pelement; num1, num2 : integer);
var tmp : pelement;
begin
  tmp := head;
  New (head);
  head^.a := num1;
  head^.b := num2;
  head^.next := tmp;
end;

function Deletea (var head : pelement):integer;
var tmp : pelement;
begin
  if (head <> nil) then
  begin
    tmp := head^.next;
    Deletea := head^.a;
    Dispose (head);
    head := tmp;
  end;
end;

function Deleteb (var head : pelement):integer;
var tmp : pelement;
begin
  if (head <> nil) then
  begin
    tmp := head^.next;
    Deleteb := head^.b;
    Dispose (head);
    head := tmp;
  end;
end;

begin
  assignfile(fin, 'input.txt');
  reset(fin);
  readln(fin, k);
  writeln('The sum of your polynomial is:  ');
  write('0');
  
  for i := 0 to k-1 do
  begin
    read(fin, l);
    readln(fin, m);
    Add(head1, l, m);
  end;

  readln(fin, n);
  for i := 0 to n-1 do
  begin
    read(fin, o);
    readln(fin, p);
    Add(head2, o, p);
  end;

  if (k>=n) then
  begin
    for i:=0 to k-1 do
    begin
      if (head1=nil) then
        break;
      if (head2=nil) then
      begin
        write('+', head1.a, 'x^', head1.b);
        head1:=head1.next;
      end;
      if (head2<>nil) then
      begin
        if (head2.b>head1.b) then
        begin
          write('+', head1.a, 'x^', head1.b);
          head1:=head1^.next;
        end;
        if (head1.b=head2.b) then
        begin
          write('+', head2.a+head1.a, 'x^', head2.b);
          head1:=head1^.next;
          head2:=head2^.next;
        end;
        if (head1.b>head2.b) then
        begin
          write('+', head2.a, 'x^', head2.b);
          head2:=head2^.next;
        end;
      end;
    end;
  end;

  if (n>k) then
  begin
    for i:=o to n-1 do
    begin
      if (head2=nil) then
        break;
      if (head1=nil) then
      begin
        write('+', head2.a, 'x^', head2.b);
        head2:=head2^.next;
      end;
      if (head1<>nil) then
      begin
        if (head1.b>head2.b) then
        begin
          write('+', head2.a, 'x^', head2.b);
          head2:=head2^.next;
        end;
        if (head1.b=head2.b) then
        begin
          write('+', head2.a+head1.a, 'x^', head2.b);
          head1:=head1^.next;
          head2:=head2^.next;
        end;
        if (head2.b>head1.b) then
        begin
          write('+', head1.a, 'x^', head1.b);
          head1:=head1^.next;
        end;
      end;
    end;
  end;



  for i := 0 to k-1 do
  begin
    Deletea(head1);
    Deleteb(head1);
  end;

  for i := 0 to n-1 do
  begin
    Deletea(head2);
    Deleteb(head2);
  end;

  closefile(fin);
  readln;
end.
